import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import '../../base/constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import './point_sub_page.dart';
import '../../entity/point_cate_model.dart';

class PointPage extends StatefulWidget {
  PointPage({Key key}) : super(key: key);
  @override
  _PointPageState createState() => _PointPageState();
}

class _PointPageState extends State<PointPage> {
  TabController _tabController;
  List<PointCateModelData> tabs = []; //顶级分类数组
  
  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: tabs.length,
      child:  Scaffold(
        appBar:  AppBar(
          backgroundColor: Colors.red,
          title: Text('积分商城'),
          bottom:  TabBar(
            isScrollable: true,
            tabs: tabs.map((item) {
              return _tabsBuilder(context, item);
            }).toList(),
            controller: _tabController,
            onTap: (index) {
              // print(tabs[index].pdd_id);
            },
          ),
        ),
        body: TabBarView(
          children: tabs.map<Widget>((item) {
            print(item.cat_name);
              return PointSubPage(cat_id:item.cat_id);
          }).toList(),
        ),
      ),
    );
  }

  Widget _tabsBuilder(BuildContext context, item) {
    return Tab(text: item.cat_name);
  }


@override
  void initState() {
    getPDDTopCatList();
    super.initState();
  }


  /*顶级列表*/
  getPDDTopCatList() async {
    FormData data = new FormData.fromMap({});
    Response<String> response =
        await HttpUtil().post(Api.URL_POINTGOODSCAT_PARENTLIST, data: data);
    Map userMap = json.decode(response.data);
    print(userMap);
    PointCateModelEntity itemEntity = new PointCateModelEntity.fromJson(userMap);
    if (itemEntity.code == 1) {
      if (mounted) {
        setState(() {
          tabs.clear();
          tabs = itemEntity.data;
        });
      }
    }
  }

  
}